package com.example.project.controller.project;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.project.annotation.Log;
import com.example.project.core.controller.BaseController;
import com.example.project.core.domain.AjaxResult;
import com.example.project.core.page.TableDataInfo;
import com.example.project.entity.Project;
import com.example.project.entity.ProjectMember;
import com.example.project.entity.ProjectRole;
import com.example.project.entity.User;
import com.example.project.enums.BusinessType;
import com.example.project.service.ProjectMemberService;

/**
 * 项目成员Controller
 * 
 * @author ruoyi
 * @date 2025-11-27
 */
@Controller
@RequestMapping("/project/member")
public class ProjectMemberController extends BaseController {

    private String prefix = "pm/project/member";

    @Autowired
    private ProjectMemberService projectMemberService;

    @RequiresPermissions("project:member:view")
    @GetMapping("/index")
    public String index(ModelMap mmap, Long projectId) {
		mmap.put("projectId", projectId);
        return prefix + "/member";
    }

    @RequiresPermissions("project:member:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProjectMember projectMember) {
        startPage();
        List<ProjectMember> list = projectMemberService.selectProjectMemberList(projectMember);
        return getDataTable(list);
	}

	@RequiresPermissions("project:member:add")
	@GetMapping("/add/{projectId}")
	public String add(ModelMap mmap, @PathVariable("projectId") Long projectId) {
		mmap.put("projectId", projectId);
		// 项目列表
		List<Project> projectList = commonService.getProjectList();
		mmap.put("projectList", projectList);
		// 用户列表
		List<User> userList = commonService.getUserList();
		mmap.put("userList", userList);
		// 角色列表
		List<ProjectRole> projectRoleList = projectRoleService.selectAllList();
		mmap.put("projectRoleList", projectRoleList);
		return prefix + "/add";
	}

    @RequiresPermissions("project:member:add")
	@Log(title = "项目成员", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(ProjectMember projectMember) {
		projectMember.setCreateBy(getLoginName());
		int result = projectMemberService.insertProjectMember(projectMember);
		return result == -1 ? error("项目成员已存在") : toAjax(result);
	}

    @RequiresPermissions("project:member:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        ProjectMember projectMember = projectMemberService.selectProjectMemberById(id);
        mmap.put("projectMember", projectMember);
		// 项目列表
		List<Project> projectList = commonService.getProjectList();
		mmap.put("projectList", projectList);
		// 用户列表
		List<User> userList = commonService.getUserList();
		mmap.put("userList", userList);
		// 角色列表
		List<ProjectRole> projectRoleList = projectRoleService.selectAllList();
		mmap.put("projectRoleList", projectRoleList);
        return prefix + "/edit";
    }

    @RequiresPermissions("project:member:edit")
    @Log(title = "项目成员", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProjectMember projectMember) {
        projectMember.setUpdateBy(getLoginName());
        int result = projectMemberService.updateProjectMember(projectMember);
        return result == -1 ? error("项目成员已存在") : toAjax(result);
    }

    @RequiresPermissions("project:member:remove")
    @Log(title = "项目成员", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(projectMemberService.deleteProjectMemberByIds(ids));
    }

    @RequiresPermissions("project:member:view")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap) {
        ProjectMember projectMember = projectMemberService.selectProjectMemberById(id);
        mmap.put("projectMember", projectMember);
        return prefix + "/detail";
    }

    @RequiresPermissions("project:member:view")
    @GetMapping("/project/{projectId}")
    @ResponseBody
    public AjaxResult getMembersByProjectId(@PathVariable("projectId") Long projectId) {
        ProjectMember projectMember = new ProjectMember();
        projectMember.setProjectId(projectId);
        List<ProjectMember> members = projectMemberService.selectProjectMemberList(projectMember);
        return AjaxResult.success(members);
    }
}